﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace GrouponDesktop
{
    class BDConnection
    {

    
        private static SqlConnection SqlCon;

        public static void Connect(string script)
        {
            //Instancio un objeto de la clase SqlConnection y se lo asigno a un atributo de la clase
            SqlCon = new SqlConnection();
            //Asigno la cadena de conexion al objeto SQLCon con la base de datos TORNEO
            SqlCon.ConnectionString = script;
            //Abro canal de conexión
            SqlCon.Open();
        }

        public static SqlDataReader QueryAndExecute(string query)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = query;
            command.Connection = SqlCon;
            //Ejecuto la consulta y almaceno el resultado en el objeto SqlDataReader
            return command.ExecuteReader();
      
        }

        /**
         * Devuelve true si la consulta devolvio algo
         * */
        public static bool execute(string query)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = query;
            command.Connection = SqlCon;
            SqlDataReader reader = command.ExecuteReader();
            bool anything = reader.HasRows;
            reader.Close();
            return anything;
        }

        public static void FillComboBox(ComboBox comboBox, string sqlCommand)
        {
            SqlDataReader myReader = QueryAndExecute(sqlCommand);

            while (myReader.Read())
            {
                comboBox.Items.Add(myReader[0].ToString());
            }
             
            myReader.Close();
        }

        public static void FillGrid(string sqlCommand,DataGridView dgv)
        {
            //daGriv = query;
            //DataSet dsGriv = new DataSet();
            SqlDataReader myReader =  QueryAndExecute(sqlCommand);

            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            ds.Tables.Add(dt);
            ds.Load(myReader, LoadOption.PreserveChanges, ds.Tables[0]);
            dgv.DataSource = ds.Tables[0];
            myReader.Close();
        }


        public static int QueryAndExecuteScalar(string query)
        {

            SqlCommand command = new SqlCommand();
            command.CommandText = query;
            command.Connection = SqlCon;
            //Ejecuto la consulta y almaceno el resultado en el objeto SqlDataReader
            return Convert.ToInt32(command.ExecuteScalar());

        }




        // DEVUELVE STRING ENCAMBIO
        public static string QueryAndExecuteScalarString(string query)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = query;
            command.Connection = SqlCon;
            //Ejecuto la consulta y almaceno el resultado en el objeto SqlDataReader
            return Convert.ToString(command.ExecuteScalar());
        }


        public static void ExecuteNonQuery (string query)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = query;
            command.Connection = SqlCon;
            //Ejecuto la consulta y almaceno el resultado en el objeto SqlDataReader
            command.ExecuteNonQuery();
        }


        public static void FillCheckedList(string sqlCommand, CheckedListBox clb)
        {
            SqlDataReader myReader = QueryAndExecute(sqlCommand);

            while (myReader.Read())
            {
                clb.Items.Add(myReader[0].ToString());
            }

            myReader.Close();
        }



        public static void CloseConnection()
        {
            SqlCon.Close();
        }
    }



    }

